/*
	J-Bird net/sourceforge/jbird/io/StringArrayStreamWriter.java

        Copyright (C) 2001-2007 Dick Repasky
	drepasky@users.sourceforge.net

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License 
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.

	You should have received a copy of the GNU General Public 
	License along with this program; if not, write to the Free Software 
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/
package net.sourceforge.jbird.io;

import java.io.Writer;
import java.io.IOException;

import net.sourceforge.jbird.util.Utils;


/**
  * Sends a stream of string arrays to
  * a Writer (FileWriter, StringWriter,
  * etc) .
  * <p>
  * A delimiter is used to separate elements
  * of arrays.  By default the delimiter is a space.
  * Use setDelimiter or the appropriate constructor
  * to alter the delimter.  Elements are quoted and quotes within them
  * escaped only if you set the quote using setDelimiterQuote.
  * <p>
  * An EndOfArray (EOA) delimiter is sent
  * at the end of each array.  By default it is a newline character.
  * It can be changed by using the method setArrayDelim.
  * <p>
  * To produce CSV output in which each array that is passed to the writer is
  * a line of output and elements of the arrays are comma-separated fields,
  * set the delimiter to a comma and use the setDelimiterQuote method
  * set the quote character used.  Elements/fields that contain either
  * the delimiter or the quote character will be quoted, and quotes within
  * those fields will be escaped.  This is how Microsoft Excel produces
  * CSV files.
  * @author Dick Repasky
*/

public class StringArrayStreamWriter extends StringArrayStream {

	Writer writer = null;
	String delim = " ";		// delimits items in array
	String EOA = System.getProperty("line.separator");  // delimits arrays
	char quotedelim = 0;
	//String quotedelim = null;	// quote to be used when
					// fields contain delimiter
					// no quoting if null

	/**
	  * Use default delimiters of space between array elements
	  * and newline between arrays
	*/
	public StringArrayStreamWriter(Writer writeto) {
		writer = writeto;
	}

	/**
	  * User provides delimiter to be used between array
	  * elements as well as that to be used between arrays.
	*/
	public StringArrayStreamWriter(Writer writeto,
					String usedelim,
					String useEOA) {
		writer = writeto;
		setDelimiter(usedelim);
		setArrayDelim(useEOA);
	}

	/**
	  * User provides delimiter to be used between array
	  * elements.  The default (newline) is used between
	  * arrays.
	*/
	public StringArrayStreamWriter(Writer writeto,
					String usedelim) {
		writer = writeto;
		setDelimiter(usedelim);
	}

	/**
	  * Set the delimiter that is to be used between array
	  * elements.
	*/
	public void setDelimiter(String newdelim) {
		if (newdelim == null) {
			delim = "";
		} else {
			delim = newdelim;
		}
	}

	/**
	  * Set the delimiter that is to be used between arrays.
	*/
	public void setArrayDelim(String newdelim) {
		if (newdelim == null) {
			EOA = "";
		} else {
			EOA = newdelim;
		}
	}

	/**
	  * Set the quote that will encase strings that
	  * contain the delimiter.  Only strings
	  * that contain the delimiter or quotes will be quoted.
          * The value null (default) turns off quoting.
	*/

	public final void setDelimiterQuote(String newquote) {
		quotedelim = newquote.charAt(0);
	}

	public final void setDelimiterQuote(char newquote) {
		quotedelim = newquote;
	}


	public void write(String[] stringa) throws IOException {
		if (stringa == null || stringa.length == 0) {
			return;
		}
		String tmp = stringa[0];
		if (tmp != null) {
			//if (quotedelim != null) {
			if (quotedelim != 0) {
		   		tmp = quoteString(tmp);
			}
			writer.write(tmp);
		}
		int i;
		int max = stringa.length;
		for (i = 1; i < max; i++) {
			writer.write(delim);
		        tmp = stringa[i];
			if (tmp != null) {
		        	//if (quotedelim != null) {
		        	if (quotedelim != 0) {
		        	   	tmp = quoteString(tmp);
		        	}
		        	writer.write(tmp);
			}
		}
		writer.write(EOA);
	}

	public void close() throws IOException {
		writer.close();
	}

	/**
	  * Enclose string in quotes if it contains the
	  * delimiter or the quote character.
	*/

	private final String quoteString(String strng) {
		if (strng.indexOf(delim) >= 0 
			|| strng.indexOf(quotedelim) >= 0) {
		   strng = Utils.escapeChar(strng, quotedelim, quotedelim);
		   strng = new StringBuffer()
				.append(quotedelim)
				.append(strng)
				.append(quotedelim)
				.toString();
		}
	  	return strng;
	}

}
